package com.txunda.zhuohired.aty.user.home;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ants.theantsgo.config.Settings;
import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshBase;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshListView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.home.CapacityAdapter;
import com.txunda.zhuohired.adapter.user.home.JobListAdapter;
import com.txunda.zhuohired.adapter.user.home.TodayNewAddressAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.home.FilterBean;
import com.txunda.zhuohired.bean.user.home.ReruitBean;
import com.txunda.zhuohired.http.pst.MemberHomePst;
import com.txunda.zhuohired.view.FlowLayout;
import com.txunda.zhuohired.view.TagAdapter;
import com.txunda.zhuohired.view.TagFlowLayout;
import com.txunda.zhuohired.weight.Tag;
import com.txunda.zhuohired.weight.TagListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/2/28 10:49
 * 功能描述：每日新职位
 * 联系方式：jiahuanchen@126.com
 */

public class TodayNewJobAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * listView
     */
    @ViewInject(R.id.lv_today_new_job)
    private PullToRefreshListView lv_today_new_job;
    /**
     * 半透明页
     */
    @ViewInject(R.id.view_black)
    private View view_black;
    /**
     * 智能排序
     */
    @ViewInject(R.id.layout_tv_today_new_capacity)
    private TextView layout_tv_today_new_capacity;
    @ViewInject(R.id.ll_today_new_capacity)
    private LinearLayout ll_today_new_capacity;
    @ViewInject(R.id.lv_today_new_capacity)
    private ListView lv_today_new_capacity;
    @ViewInject(R.id.lv_today_new_address)
    private ListView lv_today_new_address;
    /**
     * 地址
     */
    @ViewInject(R.id.layout_tv_today_new_address)
    private TextView layout_tv_today_new_address;
    @ViewInject(R.id.ll_today_new_address)
    private LinearLayout ll_today_new_address;
    /**
     * 筛选
     */
    @ViewInject(R.id.layout_tv_today_new_screen)
    private TextView layout_tv_today_new_screen;
    @ViewInject(R.id.ll_today_new_screen)
    private LinearLayout ll_today_new_screen;
    @ViewInject(R.id.tag_list_new_screen_welfare)//福利
    private TagListView tag_list_new_screen_welfare;
    @ViewInject(R.id.tag_today_new_screen_education)//学历
    private TagFlowLayout tag_today_new_screen_education;
    @ViewInject(R.id.tag_today_new_screen_pay)//薪资范围
    private TagFlowLayout tag_today_new_screen_pay;

    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    private JobListAdapter todayNewAdapter;
    private int width;

    private List<String> capacityList;
    private String[] capacityStr = {"智能排序", "薪资从高到低", "公司规模从大到小"};
    private CapacityAdapter capacityAdapter;
    private int capacity = 0;
    private int initCapacity = 1;

    private int initAddress = 1;

    private final List<Tag> mTags = new ArrayList<>();
    private ArrayList<FilterBean.DataBean.WelfareBean> welfare = new ArrayList();
    private ArrayList<FilterBean.DataBean.EducationBean> educationList = new ArrayList<>();
    private ArrayList<FilterBean.DataBean.SalaryBean> screenPayList = new ArrayList<>();
    private ArrayList<ReruitBean.DataBean> reruitBeanList = new ArrayList<>();
    private ArrayList<FilterBean.DataBean.AreaListBean> area_list = new ArrayList<>();
    private TagAdapter<FilterBean.DataBean.EducationBean> educationTagAdapter;
    private TagAdapter<FilterBean.DataBean.SalaryBean> scrennPayTagAdapter;
    private int initScreen = 1;
    private TodayNewAddressAdapter todayNewAddressAdapter;

    private MemberHomePst memberHomePst;
    private String token = "";//用户token
    private String icon = "";//图标id
    private String select_content = "";//搜索内容
    private String address_area = "";//地区筛选id
    private String salary = "";//薪金id
    private int page = 1;
    private String add = "";//地址id
    private String education = "";//学历id
    private String pay = "";//薪资id
    private String welfare_date = "";//福利
    private String tittle_name = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        //        tv_layout_white_center.setText("每日新职位");
        initView();
        lv_today_new_job.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            /**
             * 下拉刷新
             * @param refreshView
             */
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initHttp();//请求接口
            }

            /**
             * 上拉加载更多
             * @param refreshView
             */
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = page + 1;
                initHttp();//请求接口
            }
        });
    }

    @Override
    @OnClick({R.id.layout_ll_today_new_capacity, R.id.layout_ll_today_all, R.id.layout_ll_today_new_screen, R.id
            .tv_today_new_sure, R.id.tv_today_new_screen_refuse, R.id.tv_today_new_screen_sure, R.id.view_black})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.layout_ll_today_new_capacity://智能排序
                initCapacity = 2;
                initCapacity();
                break;
            case R.id.layout_ll_today_all://全部地区
                initAddress = 2;
                initAddress();
                break;
            case R.id.layout_ll_today_new_screen://筛选
                initScreen = 2;
                initScreen();
                break;
            case R.id.tv_today_new_sure://地区确认
                initClose();
                reruit();
                break;
            case R.id.tv_today_new_screen_refuse://筛选重置
                refuse();
                break;
            case R.id.tv_today_new_screen_sure://筛选确认
                initClose();
                reruit();
                break;
            case R.id.view_black://关闭
                initClose();
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_today_new_job;
    }

    @Override
    protected void initialized() {
        tittle_name = getIntent().getStringExtra("tittle_name");
        memberHomePst = new MemberHomePst(this);
        icon = getIntent().getStringExtra("icon");
        token = application.getUserInfo().get("data");


        capacityList = new ArrayList<>();
        for (int i = 0; i < capacityStr.length; i++) {
            capacityList.add(capacityStr[i]);
        }
        final LayoutInflater mInflater = LayoutInflater.from(this);
        educationTagAdapter = new TagAdapter<FilterBean.DataBean.EducationBean>(educationList) {

            @Override
            public View getView(FlowLayout parent, int position, FilterBean.DataBean.EducationBean s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tag_flow_layouttv,
                        tag_today_new_screen_education, false);
                tv.setText(s.getEducation_name());
                return tv;
            }
        };
        tag_today_new_screen_education.setAdapter(educationTagAdapter);
        scrennPayTagAdapter = new TagAdapter<FilterBean.DataBean.SalaryBean>(screenPayList) {

            @Override
            public View getView(FlowLayout parent, int position, FilterBean.DataBean.SalaryBean s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tag_flow_layouttv,
                        tag_today_new_screen_pay, false);
                tv.setText(s.getSalary());
                return tv;
            }
        };
        tag_today_new_screen_pay.setAdapter(scrennPayTagAdapter);
        todayNewAddressAdapter = new TodayNewAddressAdapter(area_list, this);
        lv_today_new_address.setAdapter(todayNewAddressAdapter);
    }

    private void reruit() {
        if (todayNewAddressAdapter.getList().size() > 0) {
            for (FilterBean.DataBean.AreaListBean bean : todayNewAddressAdapter.getList()
                    ) {
                if (bean.isSelect())
                    add = add + bean.getId() + ",";
            }
            if (add.length() > 1)
                add = add.substring(0, add.length() - 1);
        }
        if (tag_today_new_screen_education.getSelectedList().size() > 0) {
            Set<Integer> selectedList = tag_today_new_screen_education.getSelectedList();
            for (int i = 0; i < educationList.size(); i++) {
                if (selectedList.contains(i))
                    education = education + educationList.get(i).getId() + ",";
            }
            if (education.length() > 1)
                education = education.substring(0, education.length() - 1);
        }
        if (tag_today_new_screen_pay.getSelectedList().size() > 0) {
            Set<Integer> payList = tag_today_new_screen_pay.getSelectedList();
            for (int i = 0; i < screenPayList.size(); i++) {
                if (payList.contains(i))
                    pay = pay + screenPayList.get(i).getId() + ",";
            }
            if (pay.length() > 1)
                pay = pay.substring(0, pay.length() - 1);
        }
        if (tag_list_new_screen_welfare.getTags().size() > 0) {
            for (Tag tag : tag_list_new_screen_welfare.getTags()) {
                if (tag.isChecked())
                    welfare_date = welfare_date + tag.getId() + ",";
            }
            if (welfare_date.length() > 1)
                welfare_date = welfare_date.substring(0, welfare_date.length() - 1);
        }
        L.e("======String.valueOf(capacity)====", String.valueOf(capacity));
        L.e("======add====", add);
        L.e("======education====", education);
        L.e("======pay====", pay);
        L.e("======welfare_date====", welfare_date);
        page = 1;
        initHttp();
    }

    private void refuse() {
        List<Tag> tags = tag_list_new_screen_welfare.getTags();
        mTags.clear();
        for (Tag get_tag : tags) {
            Tag tag = new Tag();
            tag.setId(get_tag.getId());
            tag.setChecked(false);
            tag.setTitle(get_tag.getTitle());
            mTags.add(tag);
        }
        tag_list_new_screen_welfare.setTags(mTags);
        educationTagAdapter.setSelectedList();
        scrennPayTagAdapter.setSelectedList();
    }

    private void initHttp() {
        memberHomePst.reruit(icon, token, String.valueOf(capacity), add, education, pay, welfare_date,
                select_content, "", "", "", "", "", String.valueOf(page),"");
    }

    @Override
    protected void requestData() {
        tv_layout_white_center.setText(tittle_name);
        memberHomePst.filter(token);
        //        initAdapter();
        initHttp();
    }

    private void initAdapter() {
        todayNewAdapter = new JobListAdapter(this);
        lv_today_new_job.setAdapter(todayNewAdapter);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("filter")) {
            //            L.e("=========filter==========", jsonStr);
            FilterBean filterBean = GsonUtil.GsonToBean(jsonStr, FilterBean.class);
            area_list = filterBean.getData().getArea_list();
            welfare = filterBean.getData().getWelfare();
            educationList = filterBean.getData().getEducation();
            screenPayList = filterBean.getData().getSalary();
            mTags.clear();
            for (int i = 0; i < welfare.size(); i++) {
                Tag tag = new Tag();
                tag.setId(i);
                tag.setChecked(false);
                tag.setTitle(welfare.get(i).getWelfare_name());
                mTags.add(tag);
            }
            tag_list_new_screen_welfare.setTags(mTags);
            educationTagAdapter.setData(educationList);
            scrennPayTagAdapter.setData(screenPayList);
            todayNewAddressAdapter.setData(area_list);
            educationTagAdapter.notifyDataChanged();
            scrennPayTagAdapter.notifyDataChanged();
            todayNewAddressAdapter.notifyDataSetChanged();
            return;
        } else if (requestUrl.contains("reruit")) {
            //            L.e("==========reruit==========", jsonStr);
            lv_today_new_job.onRefreshComplete();//停止刷新
            ReruitBean reruitBean = GsonUtil.GsonToBean(jsonStr, ReruitBean.class);
            if (page == 1) {
                reruitBeanList = reruitBean.getData();
                if (reruitBeanList.size() == 0) {
                    noData(2);
                } else {
                    noData(1);
                    initAdapter();
                    todayNewAdapter.setData(reruitBeanList);
                }
            } else {
                if (reruitBean.getData().size() == 0) {
                    showToast("已经是最后一页，没有数据了");
                } else {
                    List myRewardList = reruitBean.getData();
                    reruitBeanList.addAll(myRewardList);
                    todayNewAdapter.notifyDataSetChanged();
                }
            }


        }
    }

    private void noData(int type) {
        if (type == 1) {
            lv_today_new_job.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        } else {
            lv_today_new_job.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 智能排序
     */
    private void initCapacity() {
        view_black.setVisibility(View.VISIBLE);
        ll_today_new_capacity.setVisibility(View.VISIBLE);
        ll_today_new_address.setVisibility(View.GONE);
        ll_today_new_screen.setVisibility(View.GONE);
        capacityAdapter = new CapacityAdapter(this, capacityList, capacity);
        lv_today_new_capacity.setAdapter(capacityAdapter);
        capacityAdapter.SetClickListener(new CapacityAdapter.HolderClickListener() {
            @Override
            public void onHolderClick(int position) {
                capacity = position;
                layout_tv_today_new_capacity.setText(capacityList.get(position).toString());
                initClose();
                initHttp();
            }
        });
    }

    /**
     * 地区选择
     */
    private void initAddress() {
        view_black.setVisibility(View.VISIBLE);
        ll_today_new_capacity.setVisibility(View.GONE);
        ll_today_new_address.setVisibility(View.VISIBLE);
        ll_today_new_screen.setVisibility(View.GONE);
        //        ArrayList<AddBean> list = new ArrayList<>();
        //        list.add(new AddBean("天津", false));
        //        list.add(new AddBean("北京", false));
        //        list.add(new AddBean("上海", false));
        //        list.add(new AddBean("广东", false));
        //        todayNewAddressAdapter = new TodayNewAddressAdapter(list, this);
        //        lv_today_new_address.setAdapter(todayNewAddressAdapter);
    }

    /**
     * 筛选
     */
    private void initScreen() {
        view_black.setVisibility(View.VISIBLE);
        ll_today_new_capacity.setVisibility(View.GONE);
        ll_today_new_address.setVisibility(View.GONE);
        ll_today_new_screen.setVisibility(View.VISIBLE);
        tag_list_new_screen_welfare.setTags(mTags);
        //        final LayoutInflater mInflater = LayoutInflater.from(this);
        //        tag_today_new_screen_education.setAdapter(new TagAdapter<String>(educationStr) {
        //
        //            @Override
        //            public View getView(FlowLayout parent, int position, String s) {
        //                TextView tv = (TextView) mInflater.inflate(R.layout.tag_flow_layouttv,
        //                        tag_today_new_screen_education, false);
        //                tv.setText(s);
        //                return tv;
        //            }
        //        });
        //        tag_today_new_screen_pay.setAdapter(new TagAdapter<String>(screenPayStr) {
        //
        //            @Override
        //            public View getView(FlowLayout parent, int position, String s) {
        //                TextView tv = (TextView) mInflater.inflate(R.layout.tag_flow_layouttv,
        //                        tag_today_new_screen_pay, false);
        //                tv.setText(s);
        //                return tv;
        //            }
        //        });
    }

    /**
     * 关闭
     */
    private void initClose() {
        controlType();
        view_black.setVisibility(View.GONE);
        ll_today_new_capacity.setVisibility(View.GONE);
        ll_today_new_address.setVisibility(View.GONE);
        ll_today_new_screen.setVisibility(View.GONE);
    }

    /**
     * 控制三角
     */
    private void controlType() {
        Drawable nav_up1 = getResources().getDrawable(R.mipmap.icon_today_capacity_grey);
        nav_up1.setBounds(0, 0, nav_up1.getMinimumWidth(), nav_up1.getMinimumHeight());
        Drawable nav_up2 = getResources().getDrawable(R.mipmap.icon_today_capacity_blue);
        nav_up2.setBounds(0, 0, nav_up2.getMinimumWidth(), nav_up2.getMinimumHeight());
        Drawable nav_up3 = getResources().getDrawable(R.mipmap.icon_today_screen_address_blue);
        nav_up3.setBounds(0, 0, nav_up3.getMinimumWidth(), nav_up3.getMinimumHeight());
        Drawable nav_up4 = getResources().getDrawable(R.mipmap.icon_today_screen_address_grey);
        nav_up4.setBounds(0, 0, nav_up4.getMinimumWidth(), nav_up4.getMinimumHeight());
        if (initCapacity == 1) {
            layout_tv_today_new_capacity.setCompoundDrawables(null, null, nav_up1, null);
        } else if (initCapacity == 2) {
            layout_tv_today_new_capacity.setCompoundDrawables(null, null, nav_up2, null);
        }
        if (initScreen == 1) {
            layout_tv_today_new_screen.setCompoundDrawables(null, null, nav_up4, null);
        } else if (initScreen == 2) {
            layout_tv_today_new_screen.setCompoundDrawables(null, null, nav_up3, null);
        }
        if (initAddress == 1) {
            layout_tv_today_new_address.setCompoundDrawables(null, null, nav_up4, null);
        } else if (initAddress == 2) {
            layout_tv_today_new_address.setCompoundDrawables(null, null, nav_up3, null);
        }
    }

    private void initView() {
        width = Settings.displayHeight / 2; // 计算控件宽度
        ViewGroup.LayoutParams lp = ll_today_new_capacity.getLayoutParams();
        lp.width = Settings.displayWidth;
        lp.height = width;
        ll_today_new_capacity.setLayoutParams(lp);
        ll_today_new_capacity.setBackgroundColor(getResources().getColor(R.color.white));
        ViewGroup.LayoutParams lp1 = ll_today_new_address.getLayoutParams();
        lp1.width = Settings.displayWidth;
        lp1.height = width;
        ll_today_new_address.setLayoutParams(lp1);
        ViewGroup.LayoutParams lp2 = ll_today_new_screen.getLayoutParams();
        lp2.width = Settings.displayWidth;
        lp2.height = width;
        ll_today_new_screen.setLayoutParams(lp2);
    }
}
